package com.github.butid.data;

import com.github.butid.data.codec.CmdMessage;
import com.github.butid.data.pipline.TCPClientPipline;
import com.github.butid.data.pipline.UDPClientPipline;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * Netty客户端通讯
 * @author wangjing.dc@qq.com
 * @since 2020/8/12
 */
@Slf4j
public class NettyClient {

    private static EventLoopGroup group = new NioEventLoopGroup(2);

    /**
     * 发送UDP消息
     * @param ip
     * @param port
     * @param message
     *
     * String msg = "A5FB9F50B59E19B90F54A3B1A84D05A80FCE6568C40136E218A02B4CCA39593407AA2CBDB5431B8CF6C0C63D67F0A0CB32287F96F0EF5E6F40E79B41421DBA20D7153C288316AF7C3FA15C394A44D0298210270CB89A7A9CA0C245D87B5A43ADFAC17D124F35EE49F0084D55AA9367C7C28E5D33D66520C47D244BE66D873A5F695A7D8BC36259CD9C1FD5296379F92E7BDF48D8B40F93E8F8BE4C693F75063CBBE11C107C9CD4186B185595790989E9B36A01A13CA5D871EC1553DC4DF03A3B3E49B75A793EAFBBA90936EC19177ECB5B915FA6041AB7D4C96BA3C95CDE1B393B31C97A1B7B0FD4A545C21C9A2BF972BB503E94DF4090178FD5038421443C57989B4AA098C1B74DCDB9CD21B0FB3D596370618B3B6ABE27";
     */
    public static void sendUDP(String ip, Integer port, CmdMessage message) throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new UDPClientPipline());

        ChannelFuture future = bootstrap.connect(ip, port).sync().addListener(
                (ChannelFutureListener) future1 -> {
            if(future1.isSuccess()){
                future1.channel().writeAndFlush(message);
            }else{
                future1.cause().printStackTrace();
            }
        });

    }

    /**
     * 发送TCP消息
     * @param ip
     * @param port
     * @param message
     * @param timeout
     */
    public static void send(String ip, Integer port, CmdMessage message, Integer timeout) throws Exception {

        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .remoteAddress(new InetSocketAddress(ip, port))
                .channel(NioSocketChannel.class)
//                .option(ChannelOption.AUTO_CLOSE, true)
//                .option(ChannelOption.AUTO_READ, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout)
                .handler(new TCPClientPipline());

        ChannelFuture future = bootstrap.connect(ip, port).syncUninterruptibly()
                .addListener((ChannelFutureListener) future1 -> {
                    if(future1.isSuccess()){
                        future1.channel().writeAndFlush(message);
    //                    future.channel().disconnect();
                    }else{
                        log.error("client发送消息异常", future1.cause());
                    }
                });
    }


    public static void main(String[] args) throws Exception {

        send("192.168.4.61", 7001,
//                new CmdMessage().addBody("COMD_PARA", new ArrayList<String>())
                new CmdMessage()
                , 30000);
    }

}
